package org.library.config;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.library.mapper.UserMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import javax.xml.crypto.Data;
import java.io.*;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author Chen
 * @since 2023/6/13 14:40
 */

@Configuration
@MapperScan("org.library.mapper")
public class MybatisPlusConfig {

    /**
     * 获取数据源
     * @return 数据源
     */
    public DataSource dataSourceProperties() {
        // 获取类加载器
        ClassLoader classLoader = MybatisPlusConfig.class.getClassLoader();
        // 读取配置文件
        try (InputStream resourceAsStream = classLoader.getResourceAsStream("jdbc.properties")) {
            Properties properties = new Properties();
            // 加载配置文件
            properties.load(resourceAsStream);
            // 加载驱动
            Class.forName(properties.getProperty("jdbc.driverClassName"));
            // 返回数据源
            return new PooledDataSource(
                    properties.getProperty("jdbc.driverClassName"),
                    properties.getProperty("jdbc.url"),
                    properties.getProperty("jdbc.username"),
                    properties.getProperty("jdbc.password"));
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }


    @Bean
    public SqlSessionFactory buildSqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory;
        // 准备数据源连接信息
        DataSource dataSource = dataSourceProperties();
        // 准备事务管理
        TransactionFactory transactionFactory = new JdbcTransactionFactory();

        // 构建一个环境
        Environment environment = new Environment("development", transactionFactory, dataSource);

        // 实例化配置类
        MybatisConfiguration configuration = new MybatisConfiguration(environment);

        // 配置mybatis
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        configuration.setCacheEnabled(false);

        // 获取SqlSessionFactory， 实例化结束
        sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setConfiguration(configuration);
        sqlSessionFactory.setDataSource(dataSource);
        return sqlSessionFactory.getObject();
    }

}
